package com.x.linx.fenfencard;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import com.x.linx.fenfencard.model.Manager;
import com.x.linx.fenfencard.model.User;
import com.x.linx.fenfencard.model.meta.IRequestCallback;
import com.x.linx.utils.D;


import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@EActivity(R.layout.activity_login)
public class LoginActivity extends Activity {
	private static final String TAG = LoginActivity_.class.getName();
	
	@ViewById Button btnRegister;
	@ViewById EditText etUsername;
	@ViewById EditText etPassword;
	@ViewById Button btnLogin;
	@ViewById TextView tvForgetPassword;
	// login mode
	@ViewById Button btnSwitchLoginMode;
	@ViewById LinearLayout llUserLogin;
	@ViewById LinearLayout llMobileLogin;
	boolean mIsMobileLogin = false;
	
	MyApp myApp;
	Manager mManager;

	@Click
	void btnRegister(){
		D.i("btnRegister");
	}
	@Click
	void btnLogin(){
		D.i("btnLogin");
		String username = etUsername.getText().toString();
		String password = etPassword.getText().toString();
		mManager.signin(username, password, new IRequestCallback() {
			@Override
			public void success(Object returnValueObject) {
				Toast.makeText(LoginActivity.this, "login success.", Toast.LENGTH_SHORT).show();
			}
			@Override
			public void fail(String errorMessage) {
				Toast.makeText(LoginActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
			}
		});
	}
	@Click
	void btnSwitchLoginMode(){
		D.i("btnSwitchLoginMode");
		mIsMobileLogin = !mIsMobileLogin;
		showLoginForm();
	}
	private void showLoginForm() {
		if(mIsMobileLogin){
			llMobileLogin.setVisibility(View.VISIBLE);
			llUserLogin.setVisibility(View.GONE);
			btnSwitchLoginMode.setText(R.string.username_login);
		}else{
			llMobileLogin.setVisibility(View.GONE);
			llUserLogin.setVisibility(View.VISIBLE);
			btnSwitchLoginMode.setText(R.string.mobile_login);
		}
	}
	@Click
	void tvForgetPassword(){
		D.i("tvForgetPassword");
	}
	
	
	@AfterViews
	protected void init() {
		Log.i(TAG, "init()");
		myApp = (MyApp)getApplication();
		mManager = myApp.getManager();
		showLoginForm();
	}
}
